package com.fpno4.util;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.ResultSetHandler;

import javax.sql.DataSource;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class JDBCUtils {
    public static Connection getConnection(){
        //获取连接
        try {
            InputStream resource = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
            Properties properties = new Properties();
            properties.load(resource);
            String user = properties.getProperty("user");
            String password = properties.getProperty("password");
            String url = properties.getProperty("url");
            String driverClass = properties.getProperty("driverClass");
            Class.forName(driverClass);
            Connection connection = DriverManager.getConnection(url, user, password);
            return connection;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;

    }
    public static void closeResource(Connection connection, PreparedStatement ps){
        if (ps!=null){
            try {
                ps.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (connection!=null){
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
    /*
    * c3p0数据库连接技术
    * */
    //数据库连接池提供一个即可
    private static ComboPooledDataSource cpds = new ComboPooledDataSource("helloc3p0");
    public static Connection getConnection1()throws Exception{
        Connection connection = cpds.getConnection();
        return connection;
    }

    /*
    * 使用dbcp数据库连接池技术获取数据库连接
    * */

    //创建一个dbcp数据库连接池
    private static DataSource source;
    //静态代码块随着类的加载而加载，只执行一次
    static {
        try {
            Properties pros = new Properties();
            //方式一
            //InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("dbcp.properties");
            //方式二
            FileInputStream is = new FileInputStream(new File("src/dbcp.properties"));
            pros.load(is);
            source = BasicDataSourceFactory.createDataSource(pros);

        }catch (Exception e){
            e.printStackTrace();
        }
}

    public static Connection getConnection2()throws Exception{
        Connection connection = source.getConnection();
        System.out.println(connection);
        return connection;
    }


    /*
    * 使用druid数据库连接池技术
    * */
    private static DataSource source1;
    static {
        try {
            Properties pros = new Properties();
            InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("druid.properties");
            pros.load(is);
            source1 = DruidDataSourceFactory.createDataSource(pros);
        }catch (Exception e){

        }
    }
    public static Connection getConnection3() throws SQLException {

        Connection connection = source.getConnection();
        return connection;
    }


    /*
    * 使用dbutils.jar中提供的DbUtils工具类，实现资源的关闭
    * */

    public static void closeResource1(Connection connection, Statement ps, ResultSet rs) {
        /*try {
            DbUtils.close(connection);
        }catch (Exception e){
            e.printStackTrace();
        }
        try {
            DbUtils.close(ps);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        try {
            DbUtils.close(rs);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }*/

        DbUtils.closeQuietly(connection);
        DbUtils.closeQuietly(ps);
        DbUtils.closeQuietly(rs);
    }
}
